/*
 * OPIAM Suite
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package opiam.admin.faare.unittests.config.javabeans;

import opiam.admin.faare.PropertiesManager;
import opiam.admin.faare.config.javabeans.JBTrigger;
import opiam.admin.faare.config.javabeans.JBTriggerProfile;
import opiam.admin.faare.unittests.TestResult;
import opiam.admin.faare.unittests.TestUtils;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import org.apache.commons.collections.CollectionUtils;

import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Test of the JBTriggerProfile.
 */
public class JBTriggerProfileTest extends TestCase
{
    /** Instance of logger. */
    private static Logger _logger = Logger.getLogger(JBTriggerProfileTest.class);

    /** Instance of the trace class. */
    private static TestUtils _test = new TestUtils(_logger);

    /** Mail recipient. */
    private static final String MAIL_ADDRESS = "joe.baf@opiam.org";

    /** Instance of JBTriggerProfile. */
    private JBTriggerProfile jbTriggerProfile = new JBTriggerProfile();

    /**
     * Creates a new JBTriggerProfileTest object.
     *
     * @param arg0  Parameter.
     */
    public JBTriggerProfileTest(String arg0)
    {
        super(arg0);

        try
        {
            PropertiesManager.getInstance();
        }
        catch (Exception e)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("JBTriggerProfileTest constructor error");
            msg.append(TestUtils.NEXT);
            msg.append(e.toString());
            _test.displayResultError(msg.toString());
        }
    }

    /**
     * Method of the tests suite.
     *
     * @return Tests result.
     */
    public static Test suite()
    {
        TestSuite suite = new TestSuite();

        suite.addTest(new JBTriggerProfileTest("testBegin"));

        suite.addTest(new JBTriggerProfileTest("testAddTrigger"));
        suite.addTest(new JBTriggerProfileTest("testFindPreTriggerByOpName"));
        suite.addTest(new JBTriggerProfileTest("testFindPostTriggerByOpName"));

        suite.addTest(new JBTriggerProfileTest("testEnd"));

        return suite;
    }

    /**
     * Main method of the test class.
     *
     * @param args  Sets of the parameters.
     */
    public static void main(String[] args)
    {
        junit.textui.TestRunner.run(suite());
    }

    /**
     * Method to indicate the beginning of the class test.
     *
     */
    public static void testBegin()
    {
        //  To initialize the tests statement
        TestResult.getInstance().setInitiator("JBTriggerProfileTest");

        _test.displayHeaderClass("JBTriggerProfileTest");
        assertTrue(true);
    }

    /**
     * Method to indicate the end of the class test.
     *
     */
    public static void testEnd()
    {
        _test.displayFooterClass(null);
        assertTrue(true);

        // To display the tests statement
        _test.displayTestStatement("JBTriggerProfileTest");
    }

    /**
     * Test the methods: addTrigger and getTriggers.
     *
     */
    public void testAddTrigger()
    {
        _test.displayHeaderMethod("testAddTrigger");

        try
        {
            List expectedTriggers = new ArrayList();
            List resultTriggers = new ArrayList();

            expectedTriggers.add("test_trigger1");
            jbTriggerProfile.addTrigger("test_trigger1");
            resultTriggers = jbTriggerProfile.getTriggers();

            if (CollectionUtils.isEqualCollection(expectedTriggers,
                        resultTriggers))
            {
                _test.displayResultSuccess(null);
            }
            else
            {
                _test.displayResultFailure(null);
            }
        }
        catch (Exception ex)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("Exception : ");
            msg.append(TestUtils.NEXT);
            msg.append(ex.toString());
            _test.displayResultError(msg.toString());
        }

        _test.displayFooterMethod("testAddTrigger");
    }

    /**
     * Test the methods: findPreTriggerByOpName.
     *
     */
    public void testFindPreTriggerByOpName()
    {
        _test.displayHeaderMethod("testFindPreTriggerByOpName");

        try
        {
            Map expectedPreTriggersMap = new HashMap();
            Map resultPreTriggersMap = new HashMap();
            List expectedPreTriggersListCREATE = new ArrayList();
            List resultPreTriggersListCREATE = new ArrayList();
            List expectedPreTriggersListLOAD = new ArrayList();
            List resultPreTriggersListLOAD = new ArrayList();

            JBTrigger jbTrigger1 = new JBTrigger();
            jbTrigger1.setClassname(
                "opiam.admin.faare.service.services.triggers.MailSenderTrigger");
            jbTrigger1.setOpname("CREATE");
            jbTrigger1.setName("test_trigger1");
            jbTrigger1.setPost(false);
            jbTrigger1.addParam("mail.smtp.sender.template", MAIL_ADDRESS);

            JBTrigger jbTrigger2 = new JBTrigger();
            jbTrigger2.setClassname(
                "opiam.admin.faare.service.services.triggers.MailSenderTrigger");
            jbTrigger2.setOpname("LOAD");
            jbTrigger2.setName("test_trigger2");
            jbTrigger2.setPost(false);
            jbTrigger2.addParam("mail.smtp.sender.template", MAIL_ADDRESS);

            expectedPreTriggersMap.put("CREATE", expectedPreTriggersListCREATE);
            expectedPreTriggersListCREATE.add(jbTrigger1);

            jbTriggerProfile.addPreTrigger(jbTrigger1);
            resultPreTriggersListCREATE = jbTriggerProfile.findPreTriggersByOpName(
                    "CREATE");

            expectedPreTriggersMap.put("LOAD", expectedPreTriggersListLOAD);
            expectedPreTriggersListLOAD.add(jbTrigger2);

            jbTriggerProfile.addPreTrigger(jbTrigger2);
            resultPreTriggersListLOAD = jbTriggerProfile.findPreTriggersByOpName(
                    "LOAD");

            /*CollectionUtils.isEqualCollection(expectedPreTriggersListLOAD, resultPreTriggersListLOAD);*/
            if (CollectionUtils.isEqualCollection(
                        expectedPreTriggersListCREATE,
                        resultPreTriggersListCREATE))
            {
                _test.displayResultSuccess(null);
            }
            else
            {
                _test.displayResultFailure(null);
            }
        }
        catch (Exception ex)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("Exception  : ");
            msg.append(TestUtils.NEXT);
            msg.append(ex.toString());
            _test.displayResultError(msg.toString());
        }

        _test.displayFooterMethod("testFindPreTriggerByOpName");
    }

    /**
     * Test the methods: findPostTriggerByOpName.
     *
     */
    public void testFindPostTriggerByOpName()
    {
        _test.displayHeaderMethod("testFindPostTriggerByOpName");

        try
        {
            Map expectedPostTriggersMap = new HashMap();
            Map resultPostTriggersMap = new HashMap();
            List expectedPostTriggersListCREATE = new ArrayList();
            List resultPostTriggersListCREATE = new ArrayList();
            List expectedPostTriggersListLOAD = new ArrayList();
            List resultPostTriggersListLOAD = new ArrayList();

            JBTrigger jbTrigger1 = new JBTrigger();
            jbTrigger1.setClassname(
                "opiam.admin.faare.service.services.triggers.MailSenderTrigger");
            jbTrigger1.setOpname("CREATE");
            jbTrigger1.setName("test_trigger1");
            jbTrigger1.setPost(true);
            jbTrigger1.addParam("mail.smtp.sender.template", MAIL_ADDRESS);

            JBTrigger jbTrigger2 = new JBTrigger();
            jbTrigger2.setClassname(
                "opiam.admin.faare.service.services.triggers.MailSenderTrigger");
            jbTrigger2.setOpname("LOAD");
            jbTrigger2.setName("test_trigger2");
            jbTrigger2.setPost(true);
            jbTrigger2.addParam("mail.smtp.sender.template", MAIL_ADDRESS);

            expectedPostTriggersMap.put("CREATE", expectedPostTriggersListCREATE);
            expectedPostTriggersListCREATE.add(jbTrigger1);

            jbTriggerProfile.addPostTrigger(jbTrigger1);
            resultPostTriggersListCREATE = jbTriggerProfile.findPostTriggersByOpName(
                    "CREATE");

            expectedPostTriggersMap.put("LOAD", expectedPostTriggersListLOAD);
            expectedPostTriggersListLOAD.add(jbTrigger2);

            jbTriggerProfile.addPostTrigger(jbTrigger2);
            resultPostTriggersListLOAD = jbTriggerProfile.findPostTriggersByOpName(
                    "LOAD");

            /*CollectionUtils.isEqualCollection(expectedPostTriggersListCREATE,
                resultPostTriggersListCREATE);*/
            if (CollectionUtils.isEqualCollection(
                        expectedPostTriggersListLOAD, resultPostTriggersListLOAD))
            {
                _test.displayResultSuccess(null);
            }
            else
            {
                _test.displayResultFailure(null);
            }
        }
        catch (Exception ex)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("Exception : ");
            msg.append(TestUtils.NEXT);
            msg.append(ex.toString());
            _test.displayResultError(msg.toString());
        }

        _test.displayFooterMethod("testFindPostTriggerByOpName");
    }
}
